package cn.alauncher.steclientdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.PatternMatcher;
import android.os.RemoteException;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    public static final int MSG_START_SERVER = 101;
    public static final int MSG_STOP_SERVER = 102;
    public static final int MSG_GET_INFO = 103;

    public Messenger messenger;

    private static final String ACTION = "cn.alauncher.webserver.receiver";

    private static final int CMD_VALUE_START = 1;
    private static final int CMD_VALUE_ERROR = 2;
    private static final int CMD_VALUE_STOP = 4;
    private static final int CMD_VALUE_INFO = 6;

    private static final String CMD_KEY = "CMD_KEY";
    private static final String MESSAGE_KEY = "MESSAGE_KEY";


    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION.equals(action)) {
                int cmd = intent.getIntExtra(CMD_KEY, 0);
                switch (cmd) {
                    case CMD_VALUE_START: {
                        String ip = intent.getStringExtra(MESSAGE_KEY);
                        android.util.Log.d("alauncher", "CMD_VALUE_START ip = " + ip);
                        break;
                    }
                    case CMD_VALUE_ERROR: {
                        String error = intent.getStringExtra(MESSAGE_KEY);
                        android.util.Log.d("alauncher", "CMD_VALUE_START error = " + error);
                        break;
                    }
                    case CMD_VALUE_STOP: {
                        android.util.Log.d("alauncher", "CMD_VALUE_STOP");
                        break;
                    }
                    case CMD_VALUE_INFO: {
                        String info = intent.getStringExtra(MESSAGE_KEY);
                        android.util.Log.d("alauncher", "CMD_VALUE_INFO info = " + info);
                        break;
                    }
                }
            }
        }
    };

    public void register() {
        IntentFilter filter = new IntentFilter(ACTION);
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onDestroy() {
        if (mReceiver != null) {
            this.unregisterReceiver(mReceiver);
        }

        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.bindServerBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindMessenger();
            }
        });

        findViewById(R.id.startServerBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMsg(MSG_START_SERVER);
            }
        });


        findViewById(R.id.stopServerBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMsg(MSG_STOP_SERVER);
            }
        });

        findViewById(R.id.getInfoBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendMsg(MSG_GET_INFO);
            }
        });
        register();
    }

    View.OnClickListener onClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bindServerBtn:
                    break;
                case R.id.startServerBtn:
                    break;
                case R.id.stopServerBtn:
                    break;
            }
        }
    };

    public void bindMessenger() {
        Intent messengerbinder = new Intent();
        messengerbinder.setComponent(new ComponentName(
                "cn.alauncher.data.acquisition",
                "cn.alauncher.data.acquisition.service.WebService"));
        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName name) {
                android.util.Log.d("alauncher", "onServiceDisconnected");
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                messenger = new Messenger(service);
                android.util.Log.d("alauncher", "onServiceConnected");
            }
        };
        bindService(messengerbinder, connection, Context.BIND_AUTO_CREATE);
    }

    public void sendMsg(int what) {
        if (messenger == null)
            return;
        Message msg = Message.obtain();
        msg.what = what;
        try {
            messenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

}